package org.zht.dormpro.dorm.service.impl;

import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.util.CollectionUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.bean.StudentDTO;
import org.zht.dormpro.dorm.cacher.IDormCacher;
import org.zht.dormpro.dorm.cacher.IStudentCacher;
import org.zht.dormpro.dorm.mapper.TbStudentMapper;
import org.zht.dormpro.dorm.model.TbStudent;
import org.zht.dormpro.dorm.service.IStudentService;
import org.zht.dormpro.dorm.service.base.AbsBaseService;
import org.zht.dormpro.dorm.service.base.BaseServiceCallBack;
import org.zht.dormpro.pub.util.TimeUtil;

import java.util.*;

@Service
@Log4j
public class StudentServiceImpl extends AbsBaseService implements IStudentService  {
    @Autowired
    private TbStudentMapper tbStudentMapper;
    @Autowired
    private IStudentCacher studentCacher;
    @Autowired
    private IDormCacher dormCacher;


    @Override
    public boolean addStudent(final TbStudent student) throws Exception {
        return super.handleBoolean(new BaseServiceCallBack() {
            @Override
            public boolean handle() throws Exception {
                student.setStuStatus(1);
                int count = tbStudentMapper.insertSelective(student);
                //添加学生之后要进行相关联的信息的修改
                //比如床铺添加之后不能再选择这个床铺,所以要进行删除
                Integer bedId = student.getBedId();
                List<HashMap<String, Object>> allBed = dormCacher.getAllBed(student.getDormId().toString());
                if(CollectionUtil.isNotEmpty(allBed)){
                    Iterator<HashMap<String, Object>> iterator = allBed.iterator();
                    while(iterator.hasNext()){
                        HashMap<String, Object> next = iterator.next();
                        if(next.get("id").toString().equalsIgnoreCase(bedId.toString())){
                            iterator.remove();
                            break;
                        }
                    }
                }

                return count>0;
            }
        });
    }
    @Override
    public boolean deleteStudent(final Integer stuId) throws Exception {
        return super.handleBoolean(new BaseServiceCallBack() {
            @Override
            public boolean handle() throws Exception {
                //删除缓存
                studentCacher.deleteStudent(stuId);
                //删除数据库
                int count = tbStudentMapper.deleteByPrimaryKey(stuId);
                return count>0;
            }
        });
    }
    @Override
    public boolean updateStudent(final TbStudent student) throws Exception {
        return super.handleBoolean(new BaseServiceCallBack() {
            @Override
            public boolean handle() throws Exception {
                //更新缓存
                studentCacher.deleteStudent(student.getId());
                //更新数据库
                int count = tbStudentMapper.updateByPrimaryKeySelective(student);
                return count>0;
            }
        });
    }

    @Override
    public TbStudent getStudentFromMap(Map<String, Object> studentMap){
        TbStudent tbStudent = null;
        try {
            //处理数据,用map接收是因为前台传递的日期是字符串,这里是long,也可以自由组合....
            tbStudent = BeanUtil.mapToBean(studentMap, TbStudent.class, true);
            //处理日期
            tbStudent.setInDate(TimeUtil.getTimeMillis(studentMap.get("inDate").toString()));
            //处理手机号
            tbStudent.setStuPhone(tbStudent.getStuPhone().replace("-",""));
        } catch (Exception e) {
           log.error("转换前台学生数据失败!",e);
        }
        return tbStudent;
    }

    @Override
    public List<StudentDTO> getStudentListByCondition(PageBean pageBean) {
        //其实这个也可以放到缓存里,先写到这里,后边有时间优化
        List<TbStudent> tbStudents = tbStudentMapper.getStudentListByCondition(pageBean);
        List<StudentDTO> results = new ArrayList<>();
        StudentDTO studentDTO = null;
        if(CollectionUtil.isNotEmpty(tbStudents)){
            for (TbStudent tbStudent : tbStudents) {
                studentDTO = new StudentDTO();
                BeanUtil.copyProperties(tbStudent,studentDTO);
                results.add(studentDTO);
            }
        }
        return results;
    }

}
